package com.foreveross.fetl.core.domain.freight.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.foreveross.fetl.common.jpa.EntityHibernateRepository;
import com.foreveross.fetl.core.domain.freight.OfficChannelSpreadEntity;
import com.foreveross.fetl.core.domain.freight.repository.IOfficChannelSpreadRepository;

@Repository("officChannelSpreadRepository")
public class OfficChannelSpreadRepositoryImpl extends EntityHibernateRepository
		implements IOfficChannelSpreadRepository {

	@Override
	public void removeList(List<OfficChannelSpreadEntity> list) {
		if(null!=list && !list.isEmpty()){
			for(OfficChannelSpreadEntity entity :list){
				this.delete(OfficChannelSpreadEntity.class, entity.getId());
			}
		}

	}

	@Override
	public int saveList(List<OfficChannelSpreadEntity> list) {
		if(null!=list && !list.isEmpty()){
			int i=0;
			for(OfficChannelSpreadEntity entity :list){
				entity.save();
				i++;
			}
			return i;
		}
		return 0;
	}

	@Override
	public List<OfficChannelSpreadEntity> findByCondition(Date reportDate,
			Date flightDate, String fromCity, String toCity, String flightNo,
			String channel, String hbaseKey) {
		StringBuffer jpql =new StringBuffer("select entity from OfficChannelSpreadEntity entity where 1=1 ");
		List<Object> conditionVals = new ArrayList<Object>();
		if(reportDate!=null){
			jpql.append(" and entity.reportDate =? ");
			conditionVals.add(reportDate);
		}
		if(flightDate !=null){
			jpql.append(" and entity.flightDate =? ");
			conditionVals.add(flightDate);
		}
		if(null!=fromCity && !"".equals(fromCity)){
			jpql.append(" and entity.fromCity =? ");
			conditionVals.add(fromCity);
		}
		if(null!=toCity && !"".equals(toCity)){
			jpql.append(" and entity.toCity =? ");
			conditionVals.add(toCity);
		}
		if(null!=flightNo && !"".equals(flightNo)){
			jpql.append(" and entity.flightNo =? ");
			conditionVals.add(flightNo);
		}
		if(null!=hbaseKey && !"".equals(hbaseKey)){
			jpql.append(" and entity.hbaseKey =? ");
			conditionVals.add(hbaseKey);
		}
		if(null!=channel && !"".equals(channel)){
			jpql.append(" and entity.channel =? ");
			conditionVals.add(channel);
		}
	
		jpql.append(" order by entity.grabTime desc ");
		return super.queryResult(jpql.toString(), conditionVals.toArray());
	}

}
